/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.command.component.serializer.CommandItem;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.web.command.component.ComponentConstant;
import com.inspur.edp.web.command.component.metadata.CmpMethodParamConfig;
import com.inspur.edp.web.command.component.metadata.CmpMethodRefering;
import com.inspur.edp.web.command.component.metadata.CommandItem;

import java.util.List;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class CmpMethodReferingSerializer implements ICommandItemSerializer {
    @Override
    public JsonNode serialize(CommandItem item) {
        JsonNode itemNode = (new ObjectMapper()).createObjectNode();
        CmpMethodRefering cmpMethodRefering = (CmpMethodRefering) item;
        JsonUtil.setPropValue(itemNode, ComponentConstant.CommandItemField.ITEM_TYPE, cmpMethodRefering.getItemType());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.ID, cmpMethodRefering.getId());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.CODE, cmpMethodRefering.getCode());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.NAME, cmpMethodRefering.getName());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_ID, cmpMethodRefering.getComponentId());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_CODE, cmpMethodRefering.getComponentCode());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_NAME, cmpMethodRefering.getComponentName());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_PATH, cmpMethodRefering.getComponentPath());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_ID, cmpMethodRefering.getMethodId());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_CODE, cmpMethodRefering.getMethodCode());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_NAME, cmpMethodRefering.getMethodName());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.REPLACED, cmpMethodRefering.isReplaced());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.BEFORE_EXPANSION, cmpMethodRefering.isBeforeExpansion());
        JsonUtil.setPropValue(itemNode, ComponentConstant.CmpMethodReferingField.AFTER_EXPANSION, cmpMethodRefering.isAfterExpansion());
        List<CmpMethodParamConfig> paramConfigs = cmpMethodRefering.getParamConfigs();
        if (paramConfigs == null || paramConfigs.isEmpty()) {
            return itemNode;
        }
        ArrayNode paramConfigsNode = JsonUtil.addEmptyArray(itemNode, ComponentConstant.CmpMethodReferingField.PARAM_CONFIGS);
        for (CmpMethodParamConfig config : paramConfigs) {
            JsonNode configNode = this.writeParamConfig(config);
            paramConfigsNode.add(configNode);
        }
        return itemNode;
    }

    private JsonNode writeParamConfig(CmpMethodParamConfig paramConfig) {
        JsonNode configNode = (new ObjectMapper()).createObjectNode();
        JsonUtil.setPropValue(configNode, ComponentConstant.CmpMethodParamConfigField.PARAM_CODE, paramConfig.getParamCode());
        JsonUtil.setPropValue(configNode, ComponentConstant.CmpMethodParamConfigField.PARAM_NAME, paramConfig.getParamName());
        JsonUtil.setPropValue(configNode, ComponentConstant.CmpMethodParamConfigField.PARAM_EXPRESS, paramConfig.getParamExpress());
        return configNode;
    }

    @Override
    public CommandItem deserialize(JsonNode itemNode) {
        CmpMethodRefering cmpMethodRefering = new CmpMethodRefering();
        cmpMethodRefering.setId(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.ID, String.class));
        cmpMethodRefering.setCode(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.CODE, String.class));
        cmpMethodRefering.setName(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.NAME, String.class));
        cmpMethodRefering.setComponentId(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_ID, String.class));
        cmpMethodRefering.setComponentCode(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_CODE, String.class));
        cmpMethodRefering.setComponentName(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_NAME, String.class));
        cmpMethodRefering.setComponentPath(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.COMPONENT_PATH, String.class));
        cmpMethodRefering.setMethodId(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_ID, String.class));
        cmpMethodRefering.setMethodCode(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_CODE, String.class));
        cmpMethodRefering.setMethodName(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.METHOD_NAME, String.class));
        if (JsonUtil.isExistsProp(itemNode, ComponentConstant.CmpMethodReferingField.REPLACED)) {
            cmpMethodRefering.setReplaced(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.REPLACED, boolean.class));
        }
        if (JsonUtil.isExistsProp(itemNode, ComponentConstant.CmpMethodReferingField.BEFORE_EXPANSION)) {
            cmpMethodRefering.setBeforeExpansion(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.BEFORE_EXPANSION, boolean.class));
        }
        if (JsonUtil.isExistsProp(itemNode, ComponentConstant.CmpMethodReferingField.AFTER_EXPANSION)) {
            cmpMethodRefering.setAfterExpansion(JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.AFTER_EXPANSION, boolean.class));
        }
        ArrayNode paramConfigsNode = JsonUtil.getPropValue(itemNode, ComponentConstant.CmpMethodReferingField.PARAM_CONFIGS, ArrayNode.class);
        if (paramConfigsNode == null || paramConfigsNode.isEmpty()) {
            return cmpMethodRefering;
        }
        List<CmpMethodParamConfig> paramConfigs = cmpMethodRefering.getParamConfigs();
        for (JsonNode paramConfigNode : paramConfigsNode) {
            CmpMethodParamConfig config = this.readParamConfig(paramConfigNode);
            paramConfigs.add(config);
        }
        return cmpMethodRefering;
    }

    private CmpMethodParamConfig readParamConfig(JsonNode paramConfigNode) {
        CmpMethodParamConfig paramConfig = new CmpMethodParamConfig();
        paramConfig.setParamCode(JsonUtil.getPropValue(paramConfigNode, ComponentConstant.CmpMethodParamConfigField.PARAM_CODE, String.class));
        paramConfig.setParamName(JsonUtil.getPropValue(paramConfigNode, ComponentConstant.CmpMethodParamConfigField.PARAM_NAME, String.class));
        paramConfig.setParamExpress(JsonUtil.getPropValue(paramConfigNode, ComponentConstant.CmpMethodParamConfigField.PARAM_EXPRESS, String.class));
        return paramConfig;
    }
}
